package com.itheima.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.wxpay.sdk.WXPayUtil;
import com.google.common.collect.Maps;
import com.itheima.service.WeiXinPayService;
import com.itheima.utils.HttpClient;

/**
 * @author ：yuan_he_song
 */
@Service
public class WeiXinPayServiceImpl implements WeiXinPayService {

    /** 统一下单API */
    private final String GETCODE_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder";

    /** 查询订单API */
    private final String ORDER_QUERY_URL = "https://api.mch.weixin.qq.com/pay/orderquery";


    @Value("${appid}")
    private String appid;

    @Value("${partner}")
    private String partner;

    @Value("${partnerkey}")
    private String partnerkey;

    @Override
    public Map<String, String> createNative(String out_trade_no, String total_fee) {

        // 1.封装<统一下单API>所需的参数
        Map<String,String> param = new HashMap<>();
        param.put("appid", appid);
        param.put("mch_id", partner);
        param.put("nonce_str", WXPayUtil.generateNonceStr()); // 随机字符串
        param.put("body", "云医药");
        param.put("out_trade_no", out_trade_no);// 交易订单号
        param.put("total_fee", total_fee);
        param.put("spbill_create_ip", "127.0.0.1");
        param.put("notify_url", "http://www.itcast.cn");
        param.put("trade_type", "NATIVE"); // 交易类型

        try {
            // 生成带签名的xml,partten秘钥用做生成签名用的
            String xmlParam = WXPayUtil.generateSignedXml(param, partnerkey);
            // 2.发送post请求
            HttpClient httpClient=new HttpClient(GETCODE_URL);
            httpClient.setHttps(true);
            httpClient.setXmlParam(xmlParam);
            httpClient.post();

            // 3.获取结果
            String xmlResult = httpClient.getContent();
            Map<String, String> mapResult = WXPayUtil.xmlToMap(xmlResult);
            Map<String,String> resultMap = new HashMap<>();

            // 前端生成支付二维码的链接
            resultMap.put("code_url", mapResult.get("code_url"));
            resultMap.put("out_trade_no", out_trade_no);
            resultMap.put("total_fee", total_fee);
            return resultMap;

        }catch (Exception e){
            return new HashMap<>();
        }

    }

    @Override
    public Map<String, String> queryPayStatus(String out_trade_no) {
        // 封装请求参数
        Map<String,String> requestMap = Maps.newHashMap();
        requestMap.put("appid",appid);
        requestMap.put("mch_id",partner);
        requestMap.put("out_trade_no",out_trade_no);
        requestMap.put("nonce_str",WXPayUtil.generateNonceStr());
        try {
            String xmlParam = WXPayUtil.generateSignedXml(requestMap, partnerkey);
            HttpClient httpClient = new HttpClient(ORDER_QUERY_URL);
            httpClient.setHttps(true);
            httpClient.setXmlParam(xmlParam);
            httpClient.post();
            String xmlResult = httpClient.getContent();
            Map<String, String> resultMap = WXPayUtil.xmlToMap(xmlResult);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<>();
        }
    }
}
